

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author 绛河
 * JDBC
 */
public class DatabaseConnection {

    String driver = "com.mysql.cj.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/student";
    String user = "root";
    String password = "kangshuo0902";

    public Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(driver);    //加载数据库
            conn = DriverManager.getConnection(url, user, password); //和数据库建立连接

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return conn;  //方法要记得返回一个连接对象
    }
}
